package com.example.demo.Service.impl;

import com.example.demo.Service.IShoppingService;
import com.example.demo.entity.ShoppingCart;
import com.example.demo.entity.vo.ShoppingCartVo;
import com.example.demo.mapper.ShoppingCartMapper;
import com.example.demo.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class IShoppingServiceImpl implements IShoppingService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    private ShoppingCart shoppingCart;


    @Override
    public int shoppingCartAdd(Long userId, Long productId) {
        int rs;

        shoppingCart = shoppingCartMapper.select1(userId,productId);

        if (shoppingCart == null){
            rs = shoppingCartMapper.create(userId,productId);
        }else{
            rs = shoppingCartMapper.update(userId,productId,shoppingCart.getNum()+1);
        }

        return rs;
    }

    @Override
    public List<ShoppingCartVo> list(Long userId) {
        return shoppingCartMapper.select2(userId);
    }

    @Override
    public int NumAdd(Long shoppingCartId, Long num) {
       return shoppingCartMapper.reviseNum(shoppingCartId,num);
    }

    @Override
    public int delete(Long shoppingCartId) {
        return shoppingCartMapper.delete(shoppingCartId);
    }

    @Override
    public int count(HttpServletRequest request) {
        return shoppingCartMapper.count(JWTUtil.getUserId(request.getHeader("Authorization")));
    }
}
